﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.HYCRM.BLL;
using Koonsoft.HYCRM.Model;
using MS.Internal.Xml.XPath;
using Microsoft.Office.Interop.Word;

public partial class Auditors_EnterBank : BasePage
{
    private List<CreateBankEntity> list;
    private string str = "Bank";
    private string CurrencyHkId = "1";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            list = new List<CreateBankEntity>();
            Session[str] = list;
            Session["last"] = null;

            List<ExchangeRateEntity> exchangeRates = Session["ExchangeRates"] as List<ExchangeRateEntity>;
            if (exchangeRates == null || exchangeRates.Count == 0)
            {
                exchangeRates = new AuditorsBLL().GetExchangeRate();
                Session["ExchangeRates"] = exchangeRates;
            }


            foreach (int v in Enum.GetValues(typeof(ExplainEnum)))
            {
                ExplainEnum pStatus = (ExplainEnum)v;
                dropExplain.Items.Add(new ListItem(RemarkAttribute.GetEnumRemark(pStatus), v.ToString()));
            }

            //dropCurrency.DataSource = new AuditorsBLL().GetExchangeCurrency();
            //dropCurrency.DataTextField = "CurrencyValue";
            //dropCurrency.DataValueField = "CurrencyId";
            //dropCurrency.DataBind();
            foreach (int v in Enum.GetValues(typeof(CurrencyEnum)))
            {
                CurrencyEnum pStatus = (CurrencyEnum)v;
                dropCurrency.Items.Add(new ListItem(RemarkAttribute.GetEnumRemark(pStatus), v.ToString()));
            }

            Bind(list);
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string company = Request.QueryString["CompanyID"];
        list = Session[str] as List<CreateBankEntity>;
        if (list == null || list.Count == 0)
        {
            AlertMsg("添加信息不能为空");
            return;
        }

        string msg = "";
        if (new AuditorsBLL().CreateCompanyBank(list, out msg))
        {

            string orderid = Request.QueryString["orderid"];
            Response.Redirect("SelectBank.aspx?CompanyID=" + company + "&orderid=" + orderid);
        }
        else
        {
            if (msg.ToLower().StartsWith("sql自定义异常:"))
            {
                AlertMsg(msg.ToLower().Replace("sql自定义异常:", ""));
            }
            else
            {
                AlertMsg("插入失败，请检查数据是否异常");
            }
        }


    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        list = Session[str] as List<CreateBankEntity>;
        if (list == null)
        {
            list = new List<CreateBankEntity>();
        }
        string company = Request.QueryString["CompanyID"];
        CreateBankEntity entity = new CreateBankEntity();
        entity.Amount = txtAmount.Text;
        entity.CompanyID = company;
        entity.Currency = dropCurrency.SelectedValue;
        entity.Date = txtDate.Text;
        entity.Explain = dropExplain.SelectedValue;
        entity.VoucherNo = txtVoucherNo.Text;
        string orderid = Request.QueryString["orderid"];
        entity.OrderID = orderid;

        List<ExchangeRateEntity> exchangeRates = Session["ExchangeRates"] as List<ExchangeRateEntity>;
        List<ExchangeRateEntity> ExchangeRates =
            exchangeRates.Where(fun => fun.Date.Equals(entity.Date.Substring(0, 6))
                                        && fun.Source == entity.Currency
                                        && fun.Traget == CurrencyHkId).ToList();
        if (ExchangeRates.Count < 1)
        {
            AlertMsg(string.Format("未配置日期({0}){1}兑换港币的汇率", entity.Date.Substring(0, 6), dropCurrency.SelectedItem.Text));
            txtDate.Focus();
            return;
        }

        try
        {
            if (Session["last"] == null)
            {
                double last = 0;
                if (string.IsNullOrEmpty(TextBox1.Text))
                {
                    last = 0;
                }
                else
                {
                    try
                    {
                        last = Convert.ToDouble(TextBox1.Text);
                    }
                    catch (Exception)
                    {
                        last = 0;
                    }
                }
                Session["last"] = last;
            }

            double d2 = Convert.ToDouble(Session["last"]);

            if (dropExplain.SelectedValue == "1" || dropExplain.SelectedValue == "3")
            {
                d2 = d2 + Convert.ToDouble(entity.Amount);
            }
            else
            {
                d2 = d2 - Convert.ToDouble(entity.Amount);
            }

            entity.Last = d2;
            Session["last"] = d2;
        }
        catch (Exception)
        {
            AlertMsg("金额错误，请重新输入");
            txtAmount.Focus();
            return;
        }

        entity.id = list.Count + 1;

        list.Add(entity);
        Bind(list);
        Clearn();
    }

    void Bind(List<CreateBankEntity> list)
    {
        GridView1.DataSource = list.OrderBy(fun => fun.Date);
        GridView1.DataBind();
    }

    void Clearn()
    {
        //txtVoucherNo.Text = "";
        txtDate.Text = "";
        txtAmount.Text = "";
        dropExplain.SelectedValue = "1";
        //dropCurrency.SelectedValue = "1";

        txtDate.Focus();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        list = Session[str] as List<CreateBankEntity>;
        if (list == null)
        {
            list = new List<CreateBankEntity>();
        }

        list.Remove(list.First(fun => fun.id.Equals(e.Keys[0])));

        Bind(list);
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            int Explain = Convert.ToInt32(e.Row.Cells[5].Text);
            ExplainEnum pStatus = (ExplainEnum)Explain;
            e.Row.Cells[5].Text = RemarkAttribute.GetEnumRemark(pStatus);

            int Currency = Convert.ToInt32(e.Row.Cells[3].Text);
            CurrencyEnum CStatus = (CurrencyEnum)Currency;
            e.Row.Cells[3].Text = RemarkAttribute.GetEnumRemark(CStatus);
        }

    }
}